package com.jiesen.base.modules.uploadFile.controller;

import com.jiesen.base.modules.uploadFile.entity.Result;
import com.jiesen.base.modules.uploadFile.model.Folder;
import com.jiesen.base.modules.uploadFile.service.FolderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/folder")
public class FolderController {

    @Autowired
    private FolderService folderService;

    //根据id查询子文件夹信息以及文件信息
    @GetMapping("/{id}")
    public Result findFolderAndFileById(@PathVariable("id") Integer id) {
        Map map = folderService.findFolderFileById(id);
        return Result.ok(map);
    }

    //查询所有
    @GetMapping("/findAll")
    public Result findAll() {
        List<Folder> folderAll = folderService.findFolderAll();
        return Result.ok(folderAll);
    }

    //根据id删除文件夹
    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        int i = folderService.deleteFolderById(id);
        if (i > 0) {
            return Result.ok("成功");
        }
        return Result.error("失败");
    }

    @PostMapping("/creace")
    public Result creaceFolder(Folder folder){
        int i = folderService.insert(folder);
        if (i > 0) {
            return Result.ok("成功");
        }
        return Result.error("失败");
    }

    //根据id修改文件夹名称
    @PostMapping("/edit/{id}")
    public Result updateNameById(@PathVariable("id")Integer id, Folder folder){
        folder.setId(id);
        int i = folderService.updateFolderName(folder);
        if (i > 0) {
            return Result.ok("成功");
        }
        return Result.error("失败");
    }

    //根据id查询单个文件夹属性
    @GetMapping("/view/{id}")
    public Result findFolderById(@PathVariable("id")Integer id){
        Folder folder = folderService.findFolderById(id);
        return Result.ok(folder);
    }

}
